<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="menu" content="assetMenu" />
<meta name="nav" content="0" />
<meta name="acc" content="_projectinfo" />
<title>在建公路,水运工程项目标准试验月度统计</title>
<%@ include file="/common/extjs.jsp"%>
<script type="text/javascript"
	src="<c:url value='/ext/plugin/FieldTip.js'/>" charset="utf-8"></script>
<style>
<!--
.noEditColor {
	background: #CCCCCC;
}
-->
</style>
<script type="text/javascript">
var  app ={};
Ext.onReady(function(){
 Ext.QuickTips.init();
 	var fm = Ext.form;
 	 var groupHeader = new Ext.ux.grid.ColumnHeaderGroup({
        rows: [
		[
			{header: '', colspan: 4, align: 'center'},
			{header: '施工标准试验', colspan: 6, align: 'center'},
			{header: '监理平行复合试验', colspan: 6, align: 'center'},
			{header: '', colspan: 2, align: 'center'}
		],
		[
			{header: '', colspan: 4, align: 'center'},
			{header: '标准试验次数', colspan: 2, align: 'center'},
			{header: '合格试验次数', colspan: 2, align: 'center'},
			{header: '合格率(%)', colspan: 2, align: 'center'},
			{header: '平行试验次数', colspan: 2, align: 'center'},
			{header: '符合次数', colspan: 2, align: 'center'},
			{header: '复核率(%)', colspan: 2, align: 'center'},
			{header: '', colspan: 2, align: 'center'}
		]
           ]
    });
    var cm = new Ext.grid.ColumnModel({
    	columns:[
		new Ext.grid.RowNumberer({header:'序号',width: 70}),
    	{
    		header:'标准试验项目',
    	 	dataIndex: 'checkitem',
    	 	editor:true,
            width: 120
    	}
    	,{
    		header:'检测方法和频率',
    	 	dataIndex: 'checkway',
    	 	editor:true,
            width: 120
    	},
    	{
    		header:'预计总检测点数',
    	 	dataIndex: 'countPlan',
    	 	editor:true,
            width: 120
    	},{
    		header:'本月',
    	 	dataIndex: 'countMonth',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},
    	{
    		header:'累计',
    	 	dataIndex: 'countTotal',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},{
    		header:'本月',
    	 	dataIndex: 'passMonth',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},
    	{
    		header:'累计',
    	 	dataIndex: 'passTotal',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},{
    		header:'本月',
    	 	dataIndex: 'conYield',
            width: 75,
            renderer:Jinyi.project.Format.percentRenderer
    	},{
    		header:'累计',
    	 	dataIndex: 'conYieldTotal',
            width: 75,
            renderer:Jinyi.project.Format.percentRenderer
    	},{
    		header:'本月',
    	 	dataIndex: 'scountMonth',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},{
    		header:'累计',
    	 	dataIndex: 'scountTotal',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70     
    	},{
    		header:'本月',
    	 	dataIndex: 'spassMonth',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},{
    		header:'累计',
    	 	dataIndex: 'spassTotal',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70     
    	},{
    		header:'本月',   		
    	 	dataIndex: 'superReview',
            width: 75,
            renderer:Jinyi.project.Format.percentRenderer
    	},{
    		header:'累计' ,
    	 	dataIndex: 'superReviewTotal',
    	 	 renderer:Jinyi.project.Format.percentRenderer,
            width: 75
    	},
    	{
    		header:'附件' ,
    	 	dataIndex: 'attachment',
            width: 75
    	},
    	{
    		header:'备注' ,
    	 	dataIndex: 'remark',
    	 	editor:true,
            width: 75
    	},{
    		header:'',
    	 	dataIndex: 'id',        
    	 	hidden : true,
            width: 1
    	}],
    	defaults : {
    		align : 'center',
            sortable: true    
        },	
        isCellEditable: function(col, row) {
    	//	var record = store.getAt(row);
    		return Ext.grid.ColumnModel.prototype.isCellEditable.call(this, col, row);
  		}
    });
	
    var Record = new Ext.data.Record.create([
            	  	{name:'id',type:'string'},
            	  	{name:'pid',type:'string'},//工程ID
            	  	{name:'sid',type:'string'},  //合同段Id
            	  	{name:'checkitem',type:'string'},  //标准试验项目
            	  	{name:'checkway',type:'string'}, //检测方法和频率
            	
            		{name:'countPlan',type:'float'}, 
            		{name:'countMonth',type:'float'},  
            		{name:'countTotal',type:'float'},   
            		{name:'passMonth',type:'float'},   
               	    {name:'passTotal',type:'float'}, 
               	    
               	 	{name:'conYield',type:'float'},  
            		{name:'conYieldTotal',type:'float'},   
            	    
            		{name:'scountMonth',type:'float'},  
            		{name:'scountTotal',type:'float'},   
            		{name:'spassMonth',type:'float'},   
               	    {name:'spassTotal',type:'float'}, 
               	    
            		{name:'superReview',type:'float'},   
            	    {name:'superReviewTotal',type:'float'},
            	    
            	    {name:'attachment',type:'string'},
            	    {name:"reporter",type:'string'},
            	    {name:"rechecker",type:'string'},
            	    {name:'batch',type:'string'},
            	    {name:'remark',type:'string'},
            	    {name:'delFlag',type:'string'}
  ]);
	                                  
      
      var store =new  Ext.data.Store({
    		url: appConfig.contPath + '/report/standardtest.action',
    		baseParams :{
    				projectId : '${param.projectId}'
    					},
         	    reader: new Ext.data.JsonReader({
          		totalProperty: 'totalCounts',
               	root: 'results',
               	id: 'id'
             }, Record)
    });
                                
var conSignCombo = new fm.ComboBox({
       typeAhead: true,
       triggerAction: 'all',
       transform:'conSign',
       lazyRender:true,
       listClass: 'x-combo-list-small',
       allowBlank: true,
       width :100,
       selectOnFocus : true,
       listeners : {
       	select : function(combo,record,index ){  
       		store.removeAll();
       		Ext.getCmp("batchCombo").clearValue();
       		
       		recheckerTest.setValue('');
			reporterTest.setValue('');
       		
       		batchStore.baseParams.conSignId = combo.getValue();
       		batchStore.load();
       	}
       }
   })
//统计人
var reporterTest = new Ext.form.TextField({
	allowBlank:false,
	name : 'reporter',
	width:70
});
//审核人
var recheckerTest = new Ext.form.TextField({
	allowBlank:false,
	name : 'rechecker',
	width:70
});

var  batchStore =new Ext.data.JsonStore({  //合同段批次Store
	url: appConfig.contPath + '/report/standardtest!getBatch.action',
	baseParams :{
		conSignId:conSignCombo.getValue()
	},	
	root: 'results', 
	fields:['value', 'label']	
 });

var batchCombo = new Ext.form.ComboBox({   //合同段批次combo
			typeAhead: false,
              triggerAction: 'all',
              lazyRender:false,
              editor:true,
              id:"batchCombo",
              listClass: 'x-combo-list-small',
              store: batchStore,
               mode:'remote',
               beforeBlur: Ext.emptyFn,
               triggerAction:'all',
               hideOnSelect:false,
               forceSelection : true,   
               selectOnFocus : true,
               valueField:'value',
               displayField:'label',
               triggerAction : 'all',   
               typeAhead : true,
               width :100,
               listeners : {
               	select : function(combo,record,index ){                	           		
               		store.baseParams.batch =combo.getValue();
               		store.baseParams.conSignId =conSignCombo.getValue();
               		store.load({
               			callback: function(r){
               				recheckerTest.setValue(r[0].get("rechecker"));
               				reporterTest.setValue(r[0].get("reporter"));
               			}
               		});
               	}
               }
  	});
	                                	
	function updateRow(batch){
	 	   var Plant = grid.getStore().recordType;
           var p = new Plant({
			countMonth:'',
			countTotal :'',
			passMonth: '' ,
			passTotal :'',
			batch:batch
	          });
	          grid.stopEditing();
	          store.insert(0, p);
	          grid.startEditing(0, 1);
			}
	 
	//追加
	function repairRow(){
		if(""!=batchCombo.getValue()){
			updateRow(batchCombo.getValue());
			return ;
		}
		Ext.example.msg('信息', '请选择批次!');
	}
	//新增
	function addRow(){
		store.removeAll();
		Ext.getCmp("batchCombo").clearValue();
		recheckerTest.setValue('');
		reporterTest.setValue('');
		updateRow('');
	}
	 
	var grid =new  Ext.grid.EditorGridPanel({
		cm: cm,
	       iconCls:'grid-columns-icon',
		frame:true,
		store: store,
		clicksToEdit: 1,
		title : "在建公路,水运工程项目标准试验月度统计",
		stripeRows: true,	
		listeners: {
	       	render: function(g) {    	
	       	  		//searchData();	 	
	       	},
	       	afteredit : function(e){
	       		var record = e.record;
	       		
	       		countMonth = record.get("countMonth");
	       		countTotal = record.get("countTotal");
	       		
	       		switch(e.field){
	   			case 'countTotal':
	   			case 'countMonth':
	   			case 'passMonth':
	   			case 'passTotal':{
	   				
	   				 countMonth = record.get("countMonth");
	           		 countTotal = record.get("countTotal");
	           		 passMonth = record.get("passMonth");
	           		 passTotal = record.get("passTotal");
	   				
	   				if(countMonth>countTotal){
	           			record.set("countTotal",countMonth);
	           			countTotal = record.get("countTotal");
	           		}
	   				
	   				if(passMonth>passTotal){
	   					record.set("passTotal",passMonth);
	   					passTotal =record.get("passTotal");
	   				}
	   				
	   				if(passMonth>countMonth){
	   					record.set("passMonth",countMonth);
	   					passMonth = record.get("passMonth");
	   				}
	   				
	   				if(passTotal>countTotal){
	   					record.set("passTotal",countTotal);
	   					passTotal = record.get("passTotal");
	   				}
	   				
	           		conYield = passMonth/countMonth;
	           		conYieldTotal = passTotal/countTotal;
	           		record.set("conYield",conYield);
	           		record.set("conYieldTotal",conYieldTotal);
	           		
	   				break;
	   			}
	   			
	   			case 'scountTotal':
	   			case 'scountMonth':
	   			case 'spassMonth':
	   			case 'spassTotal':{
	   				
	   				 scountMonth = record.get("scountMonth");
	           		 scountTotal = record.get("scountTotal");
	           		 spassMonth = record.get("spassMonth");
	           		 spassTotal = record.get("spassTotal");
	   				
	   				if(scountMonth>scountTotal){
	           			record.set("scountTotal",scountMonth);
	           			scountTotal = record.get("scountTotal");
	           		}
	   				
	   				if(spassMonth>spassTotal){
	   					record.set("spassTotal",spassMonth);
	   					spassTotal =record.get("spassTotal");
	   				}
	   				
	   				if(spassMonth>scountMonth){
	   					record.set("spassMonth",scountMonth);
	   					spassMonth = record.get("spassMonth");
	   				}
	   				
	   				if(spassTotal>scountTotal){
	   					record.set("spassTotal",scountTotal);
	   					spassTotal = record.get("spassTotal");
	   				}
	   				
	           	    superReview = spassMonth/scountMonth;
	           	    superReviewTotal = spassTotal/scountTotal;
	           		record.set("superReview",superReview);
	           		record.set("superReviewTotal",superReviewTotal);
	   				break;
	   			}
	   		}
	       	},
	       	delay: 10
	     	},
		sm:	new Ext.grid.RowSelectionModel({singleSelect:true}),
		plugins : [groupHeader],
		region : 'center',
	       loadMask:{msg :'数据加载中...'}, 
	       tbar:[
				'合同段：',
				conSignCombo,
				'批次',
				batchCombo,
	             	'->',
	  		   		{text: '刷新',handler:searchData,iconCls:'dt_flush'},'->',
		   		{text: '新增',handler:addRow,iconCls:'dt_flush'},'->',
		   		{text: '追加',handler:repairRow,iconCls:'dt_flush'},'->',
	  		   		{text: '删除',handler:deleteRow,iconCls:'dt_flush'},'->',
	   		   	"->",
	  		   		{text: '导出',handler:function(){exportExcelF(grid,"在建公路,水运工程项目标准试验月度统计")} ,iconCls:'bt_excel'}
			],
	       bbar:[	
	             	'统计人:',
	             	reporterTest,
	             	"-",
	             	'审核人:',
	           	recheckerTest,
	             	"->",
				{text: '保存',handler:saveMoreData,iconCls:'bt_excel'}
		]
	});
	var viewport = new Ext.Viewport({
	   layout: 'border',    
	   items: [grid]
	   }); 
                                      
/**
刷新纪录
*/
function reloadData(){
	Ext.getCmp("batchCombo").clearValue();
	batchStore.load();
	if(batchCombo.isValid()){
		recheckerTest.setValue('');
		reporterTest.setValue('');
	}
	store.removeAll();
	store.load();
	
	
}

/**
查询纪录
*/
function searchData(){
	reloadData();
}

/**
 * 删除记录
 */
 	function deleteRow(){
	 var record=grid.selModel.getSelected();
	   	if(record ==''||record==null){
	 	 	Ext.Msg.show({title: '没有选择项',msg: '请至少选择一项',modal: true,
	                	 icon: Ext.Msg.INFO,buttons: Ext.Msg.OK}); return ;}
	    Ext.Msg.confirm('删除操作', '删除将无法恢复,是否继续?', function(btn, text){
	    	if (btn == 'yes'){              	 
		    var dataId = record.get("id");
		    if(Ext.isEmpty(dataId)){
		    	store.remove(record);  
			    return;
		    } 
		   	// 数据库操作
		   	Ext.Ajax.request({
	        url: appConfig.contPath + '/report/standardtest!delete.action',
	        params:{
	         	id: dataId,
	    		mode:'json'
	        },
	         method:'POST',
	         success: function(response, options){
	        	 try {
						var responseData = Ext.decode(response.responseText);
	           			if (responseData.success == true) {
	           				reloadData();
	           	    	 Ext.example.msg('信息', '数据删除成功！');  
	            		 } else { Ext.example.msg('提示', '删除失败！');}
	            } catch (e) { Ext.example.msg('提示', '删除失败！请稍后再尝试');}
	        },
	        failure: function(response, options){Ext.example.msg('警告!', '服务端发生错误,数据删除失败!');}
	    }); 
	  }
			}); 			 
	 }

/**
 * 保存
*/
  function saveMoreData(){
   	var modified = store.getRange();
    	var len = modified.length;
      // 如果没有修改 直接返回
     	if(len == 0){ Ext.example.msg('信息', '数据没有变动,无须保存！');return;}
        if(conSignCombo.isValid()){
        	Ext.example.msg('信息', '请选择合同段！');
       		return;
        }
     	if(!reporterTest.isValid()){
     		Ext.example.msg('信息', '请填写统计人！');
     		return;
     	}
     	if(!recheckerTest.isValid()){
     		Ext.example.msg('信息', '请填写统计人！');
     		return;
}
 Ext.Msg.confirm('保存操作', '请确认数据的正确性,是否继续?', function(btn, text){
	 	if(btn == "yes"){
 	var fields = Record.prototype.fields;
 	var jsonData = '[';
	for(var i = 0 ,ilen = modified.length; i <　ilen; i++){  
		jsonData = jsonData+ '{';
		 for(var j = 0, jlen = fields.length; j < jlen; j++){
			var f = fields.items[j].name;
            var v = modified[i].get(f);
            if(!Ext.isEmpty(v)) {
               if(Ext.isDate(v)){
                 v= Jinyi.project.Format.formatDateRenderer(v);
               }
          	  jsonData = jsonData +" '" + f +　"' : '" + v +"'";
          	  if(j+1 != jlen) jsonData = jsonData+ ",";
     		}
     	}
     	jsonData = jsonData + '}';
     	var jsonStr = jsonData.substring(jsonData.length-2);
     	if(jsonStr==',}'){
     		jsonData = jsonData.substring(0,jsonData.length-2)+"}";
     	}
     	if(i+1 != ilen) jsonData = jsonData+ ",";
   	}	
	    jsonData = jsonData + ']';
      		  	
      		    // 数据库操作
      		    Ext.Ajax.request({
      		        url:  appConfig.contPath + '/report/standardtest!save.action',
      		        params: {
      		            	jsonStr: jsonData,
      		            	conSignId:conSignCombo.getValue(),
      		            	projectId:'${param.projectId}',
      		            	rechecker:recheckerTest.getValue(),
      		            	reporter:reporterTest.getValue()
      		        },
    		        method:'POST',
    		        success: function(response, options){
    		           try {
    						var responseData = Ext.decode(response.responseText);
    		            	if (responseData.success == true) {
    			            	batchStore.load();
    			            	searchData();
    			                Ext.example.msg('信息', '数据新增成功！');
    		            		}else { Ext.example.msg('提示', '数据修改失败!'); }
    								}catch(e) { Ext.example.msg('提示', '修改失败！请稍后再尝试');}
    		        },
    		        failure: function(response, options){Ext.example.msg('警告!', '服务端发生错误,数据修改失败!');}
    		    }); 
    	 	}
            });
 }
 });
 </script>
 </head>
 <body>
 <div style="display: none">
 	<pt:labels type="sign" argument1="${param.projectId}" argument2="4" name="conSign" default="${conSign}" hasEmpty="false"></pt:labels>
 </div>
 </body>
